package com.wuliaogame.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuliaogame.auto.JSONAuthentication;
import com.wuliaogame.entity.PApply;
import com.wuliaogame.entity.PBisai;
import com.wuliaogame.entity.PComment;
import com.wuliaogame.mapper.PCommentMapper;
import com.wuliaogame.mapper.PHeroMapper;
import com.wuliaogame.mapper.PUserMapper;
import com.wuliaogame.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuliaogame
 * @since 2022-04-25
 */
@RestController
@RequestMapping("/p-comment")
public class PCommentController extends JSONAuthentication {
    @Autowired
    PCommentMapper pCommentMapper;
    @Autowired
    PUserMapper pUserMapper;
    @Autowired
    PHeroMapper pHeroMapper;
    @ApiOperation(value = "添加评论")
    @PostMapping("/add")
    public void add(HttpServletRequest request, HttpServletResponse response, @RequestBody PComment pComment) throws Exception {
        if(pCommentMapper.insert(pComment)>0){
            Result ok = Result.ok().message("添加成功");
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error().message("添加失败");
            this.WriteJSON(request, response, error);
        }

    }
    @ApiOperation(value = "删除评论")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = pCommentMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据评论id查询评论")
    @PostMapping("/lookById")
    public void lookById(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        PComment pComment = pCommentMapper.selectById(id);

        if (pComment!=null) {
            Result ok = Result.ok().data("comment",pComment);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "根据评论id修改评论状态")
    @PostMapping("/updStatus")
    public void updStatus(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("status")String status) throws Exception {
        PComment pComment = pCommentMapper.selectById(id);
        pComment.setStatus(status);
        if (pCommentMapper.updateById(pComment)>0) {
            Result ok = Result.ok().message("审核成功");
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查找全部评论")
    @PostMapping("/findAllComment")
    public void findAllComment(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {

        Page<PComment> page = new Page<>(current, size);
        Page<PComment> pages = pCommentMapper.selectPage(page, null);
        List<PComment> list = pages.getRecords();
        for (PComment a: list){
            a.setPUser(pUserMapper.selectById(a.getUserId()));
            a.setPHero(pHeroMapper.selectById(a.getHeroId()));
        }
        int total = pCommentMapper.selectCount(null);
        if (list.size() > 0) {
            Result ok = Result.ok().data("apply", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有申请");
        this.WriteJSON(request, response, error);
    }

    @ApiOperation(value = "根据英雄id查找评论")
    @PostMapping("/findAllCommentByHeroId")
    public void findAllCommentByHeroId(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current,@RequestParam("heroId") int heroId) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("hero_id",heroId);
        Page<PComment> page = new Page<>(current, size);
        Page<PComment> pages = pCommentMapper.selectPage(page, wrapper);
        List<PComment> list = pages.getRecords();
        for (PComment a: list){
            a.setPUser(pUserMapper.selectById(a.getUserId()));
            a.setPHero(pHeroMapper.selectById(a.getHeroId()));
        }
        int total = pCommentMapper.selectCount(wrapper);
        if (list.size() > 0) {
            Result ok = Result.ok().data("comment", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有评论");
        this.WriteJSON(request, response, error);
    }

}

